package io.renren.modules.telnets.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qiniu.util.Json;
import io.renren.common.utils.R;
import io.renren.modules.common.dao.*;
import io.renren.modules.common.entity.AreasEntity;
import io.renren.modules.common.entity.CitiesEntity;
import io.renren.modules.common.entity.ProvincesEntity;
import io.renren.modules.common.entity.TradeCategoryEntity;
import io.renren.modules.telnets.dao.TelnetEducationBackgroundDao;
import io.renren.modules.telnets.dao.WorkTrackRecordDao;
import io.renren.modules.telnets.entity.TelnetEducationBackgroundEntity;
import io.renren.modules.telnets.entity.WorkTrackRecordEntity;
import io.renren.modules.telnets.entity.dto.TalentDTO;
import io.renren.modules.telnets.entity.dto.TalentQueryDTO;
import io.renren.modules.telnets.entity.vo.TalentsRepertoryEntityVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.telnets.dao.TalentsRepertoryDao;
import io.renren.modules.telnets.entity.TalentsRepertoryEntity;
import io.renren.modules.telnets.service.TalentsRepertoryService;


@Service("talentsRepertoryService")
public class TalentsRepertoryServiceImpl extends ServiceImpl<TalentsRepertoryDao, TalentsRepertoryEntity> implements TalentsRepertoryService {


    @Autowired
    private TelnetEducationBackgroundDao educationBackgroundDao;
    @Autowired
    private WorkTrackRecordDao workTrackRecordDao;

    @Autowired
    private ProvincesDao provincesDao;

    @Autowired
    private CitiesDao citiesDao;

    @Autowired
    private AreasDao areasDao;

    @Autowired
    private TradeCategoryDao tradeCategoryDao;

    @Autowired
    private TradeJobDao tradeJobDao;

    @Autowired
    private TradeClassifyDao tradeClassifyDao;



    @Override
    public R queryPage(Map<String, Object> params) {

        Long currentPage = Long.parseLong((String) params.get("page"));
        Long limit = Long.parseLong((String) params.get("limit"));

        String strTalentDTO = (String) params.get("queryCondition");
        TalentQueryDTO TalentDTO = Json.decode(strTalentDTO, TalentQueryDTO.class);


/*        IPage<TalentsRepertoryEntity> page = this.page(
                new Query<TalentsRepertoryEntity>().getPage(params),
                new QueryWrapper<TalentsRepertoryEntity>()
        );*/

        IPage<TalentsRepertoryEntity> page = new Page<>(currentPage,limit);


        QueryWrapper<TalentsRepertoryEntity> talentsRepertoryWrapper = new QueryWrapper<>();

        if (TalentDTO!=null){
            if (TalentDTO.getEducationId() != null){
                talentsRepertoryWrapper.eq("education_id",TalentDTO.getEducationId());
            }
            if (TalentDTO.getProvincesId()!= null
            && TalentDTO.getCitysId()!=null
            && TalentDTO.getAreasId()!= null){
                talentsRepertoryWrapper.eq("provinceid",TalentDTO.getProvincesId());
                talentsRepertoryWrapper.eq("cityid",TalentDTO.getCitysId());
                talentsRepertoryWrapper.eq("areaid",TalentDTO.getAreasId());
            }

            if (TalentDTO.getTradeId()!=null
            && TalentDTO.getClassifyId()!=null
            && TalentDTO.getJobTypeId()!=null){
                talentsRepertoryWrapper.eq("trade_id",TalentDTO.getTradeId());
                talentsRepertoryWrapper.eq("classify_id",TalentDTO.getClassifyId());
                talentsRepertoryWrapper.eq("job_type_id",TalentDTO.getJobTypeId());
            }
        }

        baseMapper.selectPage(page, talentsRepertoryWrapper);




        PageUtils pageTalents = new PageUtils(page);

        /*准备好一个容器*/
        List<TalentsRepertoryEntityVO> talentsRepertoryEntityVOS = new ArrayList<>();


        List<TalentsRepertoryEntity> talentsList = (List<TalentsRepertoryEntity>) pageTalents.getList();

        for (TalentsRepertoryEntity itemTalent :talentsList){

            TalentDTO talentDTO =  new TalentDTO();
            BeanUtils.copyProperties(itemTalent,talentDTO);


            QueryWrapper<ProvincesEntity> provincesWrapper = new QueryWrapper<>();
            provincesWrapper.eq("provinceid",itemTalent.getProvinceid());
            ProvincesEntity provincesEntity = provincesDao.selectOne(provincesWrapper);


            QueryWrapper<CitiesEntity> citiesWrapper = new QueryWrapper<>();
            citiesWrapper.eq("cityid",itemTalent.getCityid());
            CitiesEntity citiesEntity = citiesDao.selectOne(citiesWrapper);

            QueryWrapper<AreasEntity> areasWrapper = new QueryWrapper<>();
            areasWrapper.eq("areaid",itemTalent.getAreaid());
            AreasEntity areasEntity = areasDao.selectOne(areasWrapper);

            String addressStr = provincesEntity.getProvincename() + "/" +
                    citiesEntity.getCityname() + "/" + areasEntity.getAreaname();
            talentDTO.setExpAddress(addressStr);

            QueryWrapper<TradeCategoryEntity> tradeCategoryWrapper = new QueryWrapper<>();
            tradeCategoryWrapper.eq("trade_id",itemTalent.getTradeId());
            TradeCategoryEntity tradeCategoryEntity = tradeCategoryDao.selectOne(tradeCategoryWrapper);
            String industoryStr = tradeCategoryEntity.getTradeName();
            talentDTO.setExpIndustory(industoryStr);


            TalentsRepertoryEntityVO talentsRepertoryEntityVO = new TalentsRepertoryEntityVO();
            talentsRepertoryEntityVO.setTalentsRepertoryEntity(talentDTO);

            QueryWrapper<TelnetEducationBackgroundEntity> telnetEducationBackgroundWrapper = new QueryWrapper<>();
            telnetEducationBackgroundWrapper.eq("telnet_id",itemTalent.getTelnetId());
            List<TelnetEducationBackgroundEntity> telnetEducationBackgroundEntities = educationBackgroundDao.selectList(telnetEducationBackgroundWrapper);
            talentsRepertoryEntityVO.setEducationList(telnetEducationBackgroundEntities);

            QueryWrapper<WorkTrackRecordEntity> workTrackRecordWrapper = new QueryWrapper<>();
            workTrackRecordWrapper.eq("telnet_id",itemTalent.getTelnetId());
            List<WorkTrackRecordEntity> workTrackRecordEntities = workTrackRecordDao.selectList(workTrackRecordWrapper);
            talentsRepertoryEntityVO.setWorkTrackRecordList(workTrackRecordEntities);

            talentsRepertoryEntityVOS.add(talentsRepertoryEntityVO);
        }

        return R.ok().put("data",talentsRepertoryEntityVOS).put("total",page.getTotal());
        /*return new PageUtils(page);*/
    }

}